package cn.xspace.wnx.view.chat.chatrow;

import android.content.Context;
import android.text.SpannableString;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xspace.android.xafcommon.util.LogUtil;

import cn.xspace.wnx.R;
import cn.xspace.wnx.db.ChatRecordDao;
import cn.xspace.chatui.bean.MessageEntity;
import cn.xspace.chatui.bean.MsgRecordEntity;
import cn.xspace.chatui.util.ExpressionUtil;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class EaseChatRowText extends BaseChatRow {
    private static final String TAG = EaseChatRowText.class.getSimpleName();

    protected TextView contentView;

    public EaseChatRowText(Context context, MsgRecordEntity message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);
    }

    @Override
    protected void onInflateView() {
        inflater.inflate(!isMySend() ?
                R.layout.chat_received_message : R.layout.chat_row_sent_message, this);
    }

    @Override
    protected void onFindViewById() {
        contentView = (TextView) findViewById(R.id.tv_chatcontent);
    }

    @Override
    public void onSetUpView() {
//        EMTextMessageBody txtBody = (EMTextMessageBody) message;

//        Spannable span = SmileUtils.getSmiledText(context, message.msg_content);
//        // 设置内容
//        contentView.setText(span, TextView.BufferType.SPANNABLE);
        LogUtil.d(TAG, "content >>" + message.msg_content);
        SpannableString spannableString = ExpressionUtil
                .getExpressionString(context, message.msg_content, 0);
        contentView.setText(spannableString, TextView.BufferType.SPANNABLE);
//        contentView.setAutoLinkMask(Linkify.ALL); //不加也能点开链接,加了会导致抢占点击事件
//        contentView.setMovementMethod(LinkMovementMethod.getInstance());
//        contentView.setText(message.msg_content);
        handleTextMessage();
    }

    protected void handleTextMessage() {
        LogUtil.d(TAG, "wubaojie>>>handleTextMessage: " + message.msg_send_status);
        if (isMySend()) {
            if (message.msg_send_status == MessageEntity.MsgSendStatus.FAILED.toNumber()) {
                progressBar.setVisibility(View.GONE);
                statusView.setVisibility(View.VISIBLE);
            } else if (message.msg_send_status == MessageEntity.MsgSendStatus.SUCCESS.toNumber()) {
                progressBar.setVisibility(View.GONE);
                statusView.setVisibility(View.GONE);
            } else if (/*message.msg_send_status == MessageEntity.MsgSendStatus.IN_PROGRESS.toNumber() ||*/
                    message.msg_send_status == MessageEntity.MsgSendStatus.NOT_SEND.toNumber()) {
                progressBar.setVisibility(View.VISIBLE);
                statusView.setVisibility(View.GONE);
                setMessageSendCallback();
                message.msg_send_status = MessageEntity.MsgSendStatus.IN_PROGRESS.toNumber();
                registerSubscription(ChatRecordDao.getInstance().getIChatRecordOperation().updateMsgRecordSendStatus
                        (message.inserttime, MessageEntity.MsgSendStatus.IN_PROGRESS.toNumber())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe());
                //更新正在上传的状态
            }
        } else {
//            if (!message.isAcked() && message.getChatType() == ChatType.Chat) {
//                try {
//                    EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
//                } catch (HyphenateException e) {
//                    e.printStackTrace();
//                }
//            }
        }
    }

    @Override
    protected void onUpdateView() {
        super.onUpdateView();
    }

    @Override
    protected void onBubbleClick() {
    }

    @Override
    protected void buildSendMap() {
        super.buildSendMap();
//        sendMap.put("msg_text", message.msg_content);
    }
}
